﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using D3crft.Windows.UserControls;
using System.Globalization;

namespace D3crft.Windows {
    /// <summary>
    /// Interaktionslogik für wStatChooser.xaml
    /// </summary>
    public partial class wStatChooser : Window {

        public delegate void delStatHandler(int iStatID, float fStatAmmount);

        public event delStatHandler evApplyClicked;

        List<int> lStatIDs;
        List<string> lStatNames;
        UserControls.ucComboBoxAdvanced oStat;
        string sPreSetStat;
        UserControls.ucTextBox oAmmount;
        UserControls.ucButtonAdvanced oApply;

        private int iReturnStatIndex;
        UserControls.ucItemEditor.delStatReturnHandler oReturnHandler;

        bool bApplyChanges = false;

        // ####################################################
        // #####    Object Initialization
        // ####################################################
        #region Object Initialization

        public wStatChooser(object oRequester, UserControls.ucItemEditor.delStatReturnHandler oReturnHandler) {
            do_Initialize();
            iReturnStatIndex = -1;
            this.oReturnHandler = oReturnHandler;
            do_Preset();
            do_StartUp();
        }

        public wStatChooser(object oRequester, int iStatID, float fStatAmmount, int iStatIndex, UserControls.ucItemEditor.delStatReturnHandler oReturnHandler) {
            do_Initialize();
            iReturnStatIndex = iStatIndex;
            this.oReturnHandler = oReturnHandler;
            do_Preset();

            sPreSetStat = Stats.scStatFunctions.GetStatNameByID(iStatID);
            oAmmount.Text = fStatAmmount.ToString();

            do_StartUp();

        }

        private void do_Initialize() {
            InitializeComponent();

            lStatIDs = new List<int>();
            lStatNames = new List<string>();
        }

        private void do_Preset() {
            sPreSetStat = null;

            oStat = this.FindName("Stat") as ucComboBoxAdvanced;
            oAmmount = this.FindName("Ammount") as ucTextBox;
            oApply = this.FindName("Apply") as UserControls.ucButtonAdvanced;

            this.Loaded += new RoutedEventHandler(ev_Object_Loaded);
            this.Closing += new System.ComponentModel.CancelEventHandler(ev_Object_Closing);
            this.oStat.evTextChanged += new TextChangedEventHandler(ev_oStat_TextChanged);
            this.oAmmount.evTextChanged += new TextChangedEventHandler(ev_oAmmount_TextChanged);
            this.oAmmount.evKeyDown += new KeyEventHandler(ev_oAmmount_KeyDown);
        }

        private void do_StartUp() {
            do_PopulateComboBox();
        }

        #endregion

        // ####################################################
        // #####    Private Functions
        // ####################################################
        #region Private Functions

        private void do_PopulateComboBox() {
            string sTemp;

            oStat.ClearItems();
            for (int i = 0; i < 99; i++) {
                sTemp = Stats.scStatFunctions.GetStatNameByID(i);
                if (sTemp != null) {
                    oStat.AddItem(sTemp);
                    lStatIDs.Add(i);
                    lStatNames.Add(sTemp);
                }
            }
        }

        private void AllowApply() {
            oApply.Enabled = true;
        }

        private void DenyApply() {
            oApply.Enabled = false;
        }

        #endregion

        // ####################################################
        // #####    GUI Interaction
        // ####################################################
        #region GUI Interaction

        private void ev_Object_Loaded(object sender, EventArgs ea) {
            if (sPreSetStat != null) {
                oStat.Text = sPreSetStat;
                oAmmount.Focus();
                oAmmount.TextBox.SelectAll();
            }
            else {
                oStat.Focus();
            }
        }

        private void ev_Object_Closing(object sender, EventArgs ea) {
            int iStatID = -1;

            if (bApplyChanges) {
                for (int i = 0; i < lStatIDs.Count; i++) {
                    if (oStat.Text.ToLower() == lStatNames[i].ToLower()) {
                        iStatID = lStatIDs[i];
                    }
                }
                oReturnHandler(false, iStatID, float.Parse(oAmmount.Text, CultureInfo.InvariantCulture), iReturnStatIndex);
            }
            else {
                oReturnHandler(true, -1, 0.0f, -1);
            }
        }

        private void ev_oStat_TextChanged(object sender, TextChangedEventArgs ea) {
            if ((oStat.IsValidItemSelected()) && (oAmmount.Text != "")) {
                AllowApply();
            }
            else {
                DenyApply();
            }
        }

        private void ev_oAmmount_TextChanged(object sender, TextChangedEventArgs ea) {
            if ((oStat.IsValidItemSelected()) && (oAmmount.Text != "")) {
                AllowApply();
            }
            else {
                DenyApply();
            }
        }

        private void ev_oAmmount_KeyDown(object sender, KeyEventArgs ea) {
            if (ea.Key == Key.Enter) {
                if (oApply.Enabled) {
                    ev_oApply_Clicked(sender, ea);
                }
            }
        }

        private void ev_oApply_Clicked(object sender, EventArgs ea) {
            //if (evApplyClicked != null) evApplyClicked(oName.Text, oClass.Text);
            bApplyChanges = true;
            this.Close();
        }

        private void ev_oCancel_Clicked(object sender, EventArgs ea) {
            this.Close();
        }

        #endregion

    }
}
